var app = getApp()
const requireHttps = app.globalData.requireHttps //获取require域名
Page({
  data: {
    isKeyboard: !1,
    isNumberKB: !1,
    tapNum: !1,
    disableKey: "1234567890港澳学",
    keyboardNumber: "1234567890ABCDEFGHJKLMNPQRSTUVWXYZ港澳学",
    keyboard1: "京沪粤津冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘桂琼渝川贵云藏陕甘青宁新",
    inputPlates: {
      index0: "渝",
      index1: "A",
      index2: "",
      index3: "",
      index4: "",
      index5: "",
      index6: "",
      index7: ""
    },
    inputOnFocusIndex: "",
    flag: true,
    viewShow: true,
    carNumber6: '',
    carNumber7: ''
  },
  onLoad: options => {
    viewShow: (options.viewShow == "true" ? true : false)
  },
  //切换车牌
  changeplate: function() {
    let _this = this;
    app.globalData.viewShow = (!_this.data.viewShow)
    console.log(app.globalData.viewShow)
    _this.setData({
      viewShow: (!_this.data.viewShow),
      inputPlates: {
        index0: "渝",
        index1: "A",
        index2: "",
        index3: "",
        index4: "",
        index5: "",
        index6: "",
        index7: ""
      },
    })
  },
  //切换车牌
  changeplate1: function() {
    let _this = this;
    _this.setData({
      viewShow: (!_this.data.viewShow),
      // flag: true,
      inputPlates: {
        index0: "渝",
        index1: "A",
        index2: "",
        index3: "",
        index4: "",
        index5: "",
        index6: "",
      },
    })
  },
  inputClick: function(t) {
    var _this = this;
    // console.log('输入框:', t)
    _this.setData({
      inputOnFocusIndex: t.target.dataset.id,
      isKeyboard: !0
    })
    "0" == this.data.inputOnFocusIndex ? _this.setData({
      tapNum: !1,
      isNumberKB: !1
    }) : "1" == this.data.inputOnFocusIndex ? _this.setData({
      tapNum: !1,
      isNumberKB: !0
      }) : _this.setData({
      tapNum: !0,
      isNumberKB: !0
    });

  },

  //键盘点击事件
  tapKeyboard: function(t) {
    let _this = this
    t.target.dataset.index;
    var a = t.target.dataset.val;
    console.log('键盘:', a)
    if (_this.data.viewShow) {
      switch (this.data.inputOnFocusIndex) {
        case "0":
          this.setData({
            "inputPlates.index0": a,
            inputOnFocusIndex: "1"
          });
          break;

        case "1":
          this.setData({
            "inputPlates.index1": a,
            inputOnFocusIndex: "2"
          });
          break;

        case "2":
          this.setData({
            "inputPlates.index2": a,
            inputOnFocusIndex: "3"
          });
          break;

        case "3":
          this.setData({
            "inputPlates.index3": a,
            inputOnFocusIndex: "4"
          });
          break;

        case "4":
          this.setData({
            "inputPlates.index4": a,
            inputOnFocusIndex: "5"
          });
          break;

        case "5":
          this.setData({
            "inputPlates.index5": a,
            inputOnFocusIndex: "6"
          });
          break;

        case "6":
          this.setData({
            "inputPlates.index6": a,
            inputOnFocusIndex: "7"
          });
      }
      let carNumber6 = this.data.inputPlates.index0 + this.data.inputPlates.index1 + this.data.inputPlates.index2 + this.data.inputPlates.index3 + this.data.inputPlates.index4 + this.data.inputPlates.index5 + this.data.inputPlates.index6
      console.log('车牌号:', carNumber6)
      _this.data.carNumber6 = carNumber6
    } else {
      switch (this.data.inputOnFocusIndex) {
        case "0":
          this.setData({
            "inputPlates.index0": a,
            inputOnFocusIndex: "1"
          });
          break;

        case "1":
          this.setData({
            "inputPlates.index1": a,
            inputOnFocusIndex: "2"
          });
          break;

        case "2":
          this.setData({
            "inputPlates.index2": a,
            inputOnFocusIndex: "3"
          });
          break;

        case "3":
          this.setData({
            "inputPlates.index3": a,
            inputOnFocusIndex: "4"
          });
          break;

        case "4":
          this.setData({
            "inputPlates.index4": a,
            inputOnFocusIndex: "5"
          });
          break;

        case "5":
          this.setData({
            "inputPlates.index5": a,
            inputOnFocusIndex: "6"
          });
          break;

        case "6":
          this.setData({
            "inputPlates.index6": a,
            inputOnFocusIndex: "7"
          });
          break;

        case "7":
          this.setData({
            "inputPlates.index7": a,
            inputOnFocusIndex: "7"
          });
      }
      let carNumber7 = this.data.inputPlates.index0 + this.data.inputPlates.index1 + this.data.inputPlates.index2 + this.data.inputPlates.index3 + this.data.inputPlates.index4 + this.data.inputPlates.index5 + this.data.inputPlates.index6 + this.data.inputPlates.index7
      console.log('车牌号:', carNumber7)
      _this.data.carNumber7 = carNumber7
    }
    
    
    
    // this.data.carNumber = carNumber;
    this.checkedSubmitButtonEnabled();
  },
  //键盘关闭按钮点击事件
  tapSpecBtn: function(t) {
    var a = this,
      e = t.target.dataset.index;
    if (0 == e) {
      switch (parseInt(this.data.inputOnFocusIndex)) {
        case 0:
          this.setData({
            "inputPlates.index0": "",
            inputOnFocusIndex: "0"
          });
          break;

        case 1:
          this.setData({
            "inputPlates.index1": "",
            inputOnFocusIndex: "0"
          });
          break;

        case 2:
          this.setData({
            "inputPlates.index2": "",
            inputOnFocusIndex: "1"
          });
          break;

        case 3:
          this.setData({
            "inputPlates.index3": "",
            inputOnFocusIndex: "2"
          });
          break;

        case 4:
          this.setData({
            "inputPlates.index4": "",
            inputOnFocusIndex: "3"
          });
          break;

        case 5:
          this.setData({
            "inputPlates.index5": "",
            inputOnFocusIndex: "4"
          });
          break;

        case 6:
          this.setData({
            "inputPlates.index6": "",
            inputOnFocusIndex: "5"
          });
          break;

        case 7:
          this.setData({
            "inputPlates.index7": "",
            inputOnFocusIndex: "6"
          });
      }
      this.checkedSubmitButtonEnabled();
    } else 1 == e && a.setData({
      isKeyboard: !1,
      isNumberKB: !1,
      inputOnFocusIndex: ""
    });
  },
  //键盘切换
  checkedKeyboard: function() {
    var t = this;
    "0" == this.data.inputOnFocusIndex ? t.setData({
      tapNum: !1,
      isNumberKB: !1
    }) : "1" == this.data.inputOnFocusIndex ? t.setData({
      tapNum: !1,
      isNumberKB: !0
    }) : this.data.inputOnFocusIndex.length > 0 && t.setData({
      tapNum: !0,
      isNumberKB: !0
    });
  },

  checkedSubmitButtonEnabled: function() {
    this.checkedKeyboard();
    var t = !0;
    for (var a in this.data.inputPlates)
      if ("index7" != a && this.data.inputPlates[a].length < 1) {
        t = !1;
        break;
      }
  },
  yesBindCarNum: function() {
    
    let _this = this;
    console.log(_this.data.carNumber6)
    console.log(_this.data.carNumber7)
    if (_this.data.viewShow) {
      console.log(_this.data.carNumber6)
      if (_this.data.carNumber6.length == 7) {
        console.log('111')
        //新增7位车辆
        wx.request({
          url: '' + requireHttps + '/api/member/addUserLicensePlate',
          data: {
            token: app.globalData.token,
            card: _this.data.carNumber6
          },
          method: 'post',
          success: res => {
            console.log(res);
            _this.data.carNumber6 = ''
            wx.showModal({
              title: '提示',
              content: res.data.message,
              success: res => {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.navigateBack({
                    delta: 1
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                  wx.navigateBack({
                    delta: 1
                  })
                }
              }
            })
            
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '您输入的车牌号有误',
          success: res => {
            if (res.confirm) {
              
            } else if (res.cancel) {
            }
          }
        })
      }
      
    } else {
      //新增8位车辆
      console.log(_this.data.carNumber6.length)
      if (_this.data.carNumber7.length == 8) {
        console.log('222')
        wx.request({
          url: '' + requireHttps + '/api/member/addUserLicensePlate',
          data: {
            token: app.globalData.token,
            card: _this.data.carNumber7
          },
          method: 'post',
          success: res => {
            console.log(res);
            _this.data.carNumber7 = ''
            wx.showModal({
              title: '提示',
              content: res.data.message,
              success: res => {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1
                  })   
                } else if (res.cancel) {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              }
            })
            
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '您输入的车牌号有误',
          success: res => {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    }
  }
})